home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / OutOfPhase1.01Source / OutOfPhase Folder / OscillatorSpecifier.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  11.3 KB  |  379 lines  |  [TEXT/KAHL]

  1. /* OscillatorSpecifier.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "OscillatorSpecifier.h"
  31. #include "Memory.h"
  32. #include "SampleSelector.h"
  33. #include "ModulationSpecifier.h"
  34. #include "Envelope.h"
  35. #include "LFOListSpecifier.h"
  36.  
  37.  
  38. struct OscillatorRec
  39.     {
  40.         /* what is the name by which we can refer to this oscillator */
  41.         char*                                            OscillatorName;
  42.  
  43.         /* what kind of oscillator is it */
  44.         OscillatorTypes                        OscillatorType;
  45.  
  46.         /* mapping from pitch to sample */
  47.         struct SampleSelectorRec*    SampleIntervalList;
  48.  
  49.         /* list of oscillators that are modulating us and how */
  50.         struct ModulationSpecRec*    ModulatorInputList;
  51.  
  52.         /* this scales our output so that we can be set relative to other oscillators */
  53.         OscillatorNumType                    OutputLoudness;
  54.  
  55.         /* these are used to make our frequency a multiple of the instrument's */
  56.         /* overall frequency */
  57.         OscillatorNumType                    FrequencyAdjustMultiplier;
  58.         long                                            FrequencyAdjustDivisor;
  59.  
  60.         /* this switch turns on or off our output, so that we can be used only for */
  61.         /* modulation, if desired.  (True = do use, False = don't use) */
  62.         MyBoolean                                    MixInFinalOutput;
  63.  
  64.         /* this envelope determines the total output level with time */
  65.         struct EnvelopeRec*                LoudnessEnvelope;
  66.  
  67.         /* this LFO list modulates the output of the loudness envelope */
  68.         struct LFOListSpecRec*        LoudnessLFOList;
  69.  
  70.         /* this envelope determines the wave table selection index with time.  this is */
  71.         /* only used for wave table synthesis, not for sampling */
  72.         struct EnvelopeRec*                ExcitationEnvelope;
  73.  
  74.         /* this LFO list modulates the output of the excitation envelope.  it is only */
  75.         /* used for wave table synthesis. */
  76.         struct LFOListSpecRec*        ExcitationLFOList;
  77.  
  78.         /* stereo bias -- fixed amount to move this oscillator left or right by */
  79.         OscillatorNumType                    StereoBias;
  80.         /* surround bias -- fixed amount to move this oscillator front or back by */
  81.         OscillatorNumType                    SurroundBias;
  82.  
  83.         /* time displacement -- how much earlier / later to start sample */
  84.         OscillatorNumType                    TimeDisplacement;
  85.     };
  86.  
  87.  
  88. /* create a new oscillator structure */
  89. OscillatorRec*                        NewOscillatorSpecifier(void)
  90.     {
  91.         OscillatorRec*                    Osc;
  92.  
  93.         Osc = (OscillatorRec*)AllocPtrCanFail(sizeof(OscillatorRec),"OscillatorRec");
  94.         if (Osc == NIL)
  95.             {
  96.              FailurePoint1:
  97.                 return NIL;
  98.             }
  99.         Osc->OscillatorType = eOscillatorSampled; /* default -- this is kinda ugly */
  100.         Osc->OscillatorName = AllocPtrCanFail(0,"OscillatorName");
  101.         if (Osc->OscillatorName == NIL)
  102.             {
  103.              FailurePoint2:
  104.                 ReleasePtr((char*)Osc);
  105.                 goto FailurePoint1;
  106.             }
  107.         Osc->SampleIntervalList = NewSampleSelectorList(0);
  108.         if (Osc->SampleIntervalList == NIL)
  109.             {
  110.              FailurePoint3:
  111.                 ReleasePtr(Osc->OscillatorName);
  112.                 goto FailurePoint2;
  113.             }
  114.         Osc->ModulatorInputList = NewModulationSpecifier();
  115.         if (Osc->ModulatorInputList == NIL)
  116.             {
  117.              FailurePoint4:
  118.                 DisposeSampleSelectorList(Osc->SampleIntervalList);
  119.                 goto FailurePoint3;
  120.             }
  121.         Osc->OutputLoudness = 0;
  122.         Osc->FrequencyAdjustMultiplier = 1;
  123.         Osc->FrequencyAdjustDivisor = 1;
  124.         Osc->MixInFinalOutput = True;
  125.         Osc->LoudnessEnvelope = NewEnvelope();
  126.         if (Osc->LoudnessEnvelope == NIL)
  127.             {
  128.              FailurePoint5:
  129.                 DisposeModulationSpecifier(Osc->ModulatorInputList);
  130.                 goto FailurePoint4;
  131.             }
  132.         Osc->LoudnessLFOList = NewLFOListSpecifier();
  133.         if (Osc->LoudnessLFOList == NIL)
  134.             {
  135.              FailurePoint6:
  136.                 DisposeEnvelope(Osc->LoudnessEnvelope);
  137.                 goto FailurePoint5;
  138.             }
  139.         Osc->ExcitationEnvelope = NewEnvelope();
  140.         if (Osc->ExcitationEnvelope == NIL)
  141.             {
  142.              FailurePoint7:
  143.                 DisposeLFOListSpecifier(Osc->LoudnessLFOList);
  144.                 goto FailurePoint6;
  145.             }
  146.         Osc->ExcitationLFOList = NewLFOListSpecifier();
  147.         if (Osc->ExcitationLFOList == NIL)
  148.             {
  149.              FailurePoint8:
  150.                 DisposeEnvelope(Osc->ExcitationEnvelope);
  151.                 goto FailurePoint7;
  152.             }
  153.         Osc->StereoBias = 0;
  154.         Osc->TimeDisplacement = 0;
  155.         return Osc;
  156.     }
  157.  
  158.  
  159. /* dispose of an oscillator structure */
  160. void                                            DisposeOscillatorSpecifier(OscillatorRec* Osc)
  161.     {
  162.         CheckPtrExistence(Osc);
  163.         ReleasePtr(Osc->OscillatorName);
  164.         DisposeSampleSelectorList(Osc->SampleIntervalList);
  165.         DisposeModulationSpecifier(Osc->ModulatorInputList);
  166.         DisposeEnvelope(Osc->LoudnessEnvelope);
  167.         DisposeLFOListSpecifier(Osc->LoudnessLFOList);
  168.         DisposeEnvelope(Osc->ExcitationEnvelope);
  169.         DisposeLFOListSpecifier(Osc->ExcitationLFOList);
  170.         ReleasePtr((char*)Osc);
  171.     }
  172.  
  173.  
  174. /* get the actual name of the oscillator.  don't dispose this! */
  175. char*                                            OscillatorGetName(OscillatorRec* Osc)
  176.     {
  177.         CheckPtrExistence(Osc);
  178.         return Osc->OscillatorName;
  179.     }
  180.  
  181.  
  182. /* put a new oscillator name in.  the object becomes owner of the name block */
  183. void                                            PutOscillatorName(OscillatorRec* Osc, char* NewName)
  184.     {
  185.         CheckPtrExistence(Osc);
  186.         CheckPtrExistence(NewName);
  187.         ReleasePtr(Osc->OscillatorName);
  188.         Osc->OscillatorName = NewName;
  189.     }
  190.  
  191.  
  192. /* set the oscillator type */
  193. void                                            OscillatorSetTheType(OscillatorRec* Osc,
  194.                                                         OscillatorTypes WhatKindOfOscillator)
  195.     {
  196.         ERROR((WhatKindOfOscillator != eOscillatorSampled)
  197.             && (WhatKindOfOscillator != eOscillatorWaveTable),PRERR(ForceAbort,
  198.             "NewOscillatorSpecifier:  bad oscillator type number"));
  199.         CheckPtrExistence(Osc);
  200.         Osc->OscillatorType = WhatKindOfOscillator;
  201.     }
  202.  
  203.  
  204. /* find out what kind of oscillator this is */
  205. OscillatorTypes                        OscillatorGetWhatKindItIs(OscillatorRec* Osc)
  206.     {
  207.         CheckPtrExistence(Osc);
  208.         return Osc->OscillatorType;
  209.     }
  210.  
  211.  
  212. /* get the pitch interval --> sample mapping */
  213. struct SampleSelectorRec*    OscillatorGetSampleIntervalList(OscillatorRec* Osc)
  214.     {
  215.         CheckPtrExistence(Osc);
  216.         return Osc->SampleIntervalList;
  217.     }
  218.  
  219.  
  220. /* get the list of oscillators that are modulating us */
  221. struct ModulationSpecRec*    OscillatorGetModulatorInputList(OscillatorRec* Osc)
  222.     {
  223.         CheckPtrExistence(Osc);
  224.         return Osc->ModulatorInputList;
  225.     }
  226.  
  227.  
  228. /* get the output loudness of the oscillator */
  229. OscillatorNumType                    OscillatorGetOutputLoudness(OscillatorRec* Osc)
  230.     {
  231.         CheckPtrExistence(Osc);
  232.         return Osc->OutputLoudness;
  233.     }
  234.  
  235.  
  236. /* put a new output loudness in for the oscillator */
  237. void                                            PutOscillatorNewOutputLoudness(OscillatorRec* Osc,
  238.                                                         double NewOutputLevel)
  239.     {
  240.         CheckPtrExistence(Osc);
  241.         Osc->OutputLoudness = NewOutputLevel;
  242.     }
  243.  
  244.  
  245. /* get the frequency multiplier factor */
  246. OscillatorNumType                    OscillatorGetFrequencyMultiplier(OscillatorRec* Osc)
  247.     {
  248.         CheckPtrExistence(Osc);
  249.         return Osc->FrequencyAdjustMultiplier;
  250.     }
  251.  
  252.  
  253. /* get the frequency divisor integer */
  254. long                                            OscillatorGetFrequencyDivisor(OscillatorRec* Osc)
  255.     {
  256.         CheckPtrExistence(Osc);
  257.         return Osc->FrequencyAdjustDivisor;
  258.     }
  259.  
  260.  
  261. /* change the frequency adjust factors */
  262. void                                            PutOscillatorNewFrequencyFactors(OscillatorRec* Osc,
  263.                                                         double NewMultipler, long NewDivisor)
  264.     {
  265.         CheckPtrExistence(Osc);
  266.         Osc->FrequencyAdjustMultiplier = NewMultipler;
  267.         Osc->FrequencyAdjustDivisor = NewDivisor;
  268.     }
  269.  
  270.  
  271. /* find out if output of this oscillator is to be included in final output */
  272. MyBoolean                                    IncludeOscillatorInFinalOutput(OscillatorRec* Osc)
  273.     {
  274.         CheckPtrExistence(Osc);
  275.         return Osc->MixInFinalOutput;
  276.     }
  277.  
  278.  
  279. /* change whether or not the oscillator is being included in the final output */
  280. void                                            PutOscillatorIncludeInOutputFlag(OscillatorRec* Osc,
  281.                                                         MyBoolean IncludeInOutputFlag)
  282.     {
  283.         CheckPtrExistence(Osc);
  284.         Osc->MixInFinalOutput = IncludeInOutputFlag;
  285.     }
  286.  
  287.  
  288. /* get the loudness envelope for the oscillator */
  289. struct EnvelopeRec*                OscillatorGetLoudnessEnvelope(OscillatorRec* Osc)
  290.     {
  291.         CheckPtrExistence(Osc);
  292.         return Osc->LoudnessEnvelope;
  293.     }
  294.  
  295.  
  296. /* get the list of LFO oscillators modulating the loudness envelope output */
  297. struct LFOListSpecRec*        OscillatorGetLoudnessLFOList(OscillatorRec* Osc)
  298.     {
  299.         CheckPtrExistence(Osc);
  300.         return Osc->LoudnessLFOList;
  301.     }
  302.  
  303.  
  304. /* get the excitation envelope for the oscillator */
  305. struct EnvelopeRec*                OscillatorGetExcitationEnvelope(OscillatorRec* Osc)
  306.     {
  307.         CheckPtrExistence(Osc);
  308.         return Osc->ExcitationEnvelope;
  309.     }
  310.  
  311.  
  312. /* get the list of LFO oscillators modulating the excitation envelope output */
  313. struct LFOListSpecRec*        OscillatorGetExcitationLFOList(OscillatorRec* Osc)
  314.     {
  315.         CheckPtrExistence(Osc);
  316.         return Osc->ExcitationLFOList;
  317.     }
  318.  
  319.  
  320. /* resolve modulator named references to oscillators */
  321. MyBoolean                                    ResolveOscillatorModulators(OscillatorRec* Osc,
  322.                                                         struct OscillatorListRec* ListOfOscillators)
  323.     {
  324.         CheckPtrExistence(Osc);
  325.         CheckPtrExistence(ListOfOscillators);
  326.         return ResolveModulationReferences(Osc->ModulatorInputList,ListOfOscillators);
  327.     }
  328.  
  329.  
  330. /* get the stereo bias factor */
  331. OscillatorNumType                    OscillatorGetStereoBias(OscillatorRec* Osc)
  332.     {
  333.         CheckPtrExistence(Osc);
  334.         return Osc->StereoBias;
  335.     }
  336.  
  337.  
  338. /* put a new value for the stereo bias factor */
  339. void                                            OscillatorPutStereoBias(OscillatorRec* Osc,
  340.                                                         OscillatorNumType NewStereoBias)
  341.     {
  342.         CheckPtrExistence(Osc);
  343.         Osc->StereoBias = NewStereoBias;
  344.     }
  345.  
  346.  
  347. /* get the surround bias factor */
  348. OscillatorNumType                    OscillatorGetSurroundBias(OscillatorRec* Osc)
  349.     {
  350.         CheckPtrExistence(Osc);
  351.         return Osc->SurroundBias;
  352.     }
  353.  
  354.  
  355. /* put a new value for the surround bias factor */
  356. void                                            OscillatorPutSurroundBias(OscillatorRec* Osc,
  357.                                                         OscillatorNumType NewSurroundBias)
  358.     {
  359.         CheckPtrExistence(Osc);
  360.         Osc->SurroundBias = NewSurroundBias;
  361.     }
  362.  
  363.  
  364. /* get the time displacement factor */
  365. OscillatorNumType                    OscillatorGetTimeDisplacement(OscillatorRec* Osc)
  366.     {
  367.         CheckPtrExistence(Osc);
  368.         return Osc->TimeDisplacement;
  369.     }
  370.  
  371.  
  372. /* put a new value for the time displacement factor */
  373. void                                            OscillatorPutTimeDisplacement(OscillatorRec* Osc,
  374.                                                         OscillatorNumType NewTimeDisplacement)
  375.     {
  376.         CheckPtrExistence(Osc);
  377.         Osc->TimeDisplacement = NewTimeDisplacement;
  378.     }
  379.